package com.wsd.jing.controller.system;

import com.wsd.jing.annotation.ControllerLog;
import com.wsd.jing.dto.UserRoleDTO;
import com.wsd.jing.entity.UserRole;
import com.wsd.jing.request.CommonRequest;
import com.wsd.jing.request.CommonResponse;
import com.wsd.jing.service.IUserRoleService;
import com.wsd.jing.validator.group.InsertGroup;
import com.wsd.jing.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @author tom
 * @version 1.0.0
 * @projectName: jing
 * @packageName: com.wsd.jing.controller
 * @className: UserRoleController
 * @description: 用户角色关联前端控制器
 * @createDate 2022-11-29 04:06
 * @copyright 2022-11-29 04:06 www.tom.com Inc. All rights reserved.
 */
@Api(tags = "用户角色关联")
@Validated
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/rest/user-role")
public class UserRoleController {

    private final IUserRoleService userRoleService;

    @ApiOperation(value = "新增")
    @PostMapping()
    @ControllerLog(action = "新增用户角色关联", message = "新增用户角色关联失败")
    // @SaCheckPermission("userRole:add")
    public CommonResponse insertUserRole(@Validated(InsertGroup.class) @RequestBody UserRole userRole) {
        return CommonResponse.success(userRoleService.insertUserRole(userRole), "新增成功");
    }

    @ApiOperation(value = "修改")
    @PutMapping()
    @ControllerLog(action = "修改用户角色关联", message = "修改用户角色关联失败")
    // @SaCheckPermission("userRole:update")
    public CommonResponse updateUserRole(@Validated(UpdateGroup.class) @RequestBody UserRole userRole) {
        return CommonResponse.success(userRoleService.updateUserRole(userRole), "修改成功");
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "id", value = "用户角色关联id", paramType = "path", dataType = "Integer", required = true)
    @DeleteMapping("/{id}")
    @ControllerLog(action = "删除用户角色关联", message = "删除用户角色关联失败")
    // @SaCheckPermission("userRole:delete")
    public CommonResponse deleteUserRole(@PathVariable("id") Integer id) {
        return CommonResponse.success(userRoleService.deleteUserRole(id), "删除成功");
    }

    @ApiOperation(value = "批量删除")
    @ApiImplicitParam(name = "ids", value = "用户角色关联id逗号分割字符", paramType = "query", dataType = "String", required = true)
    @DeleteMapping("")
    @ControllerLog(action = "批量删除用户角色关联", message = "批量删除用户角色关联失败")
    // @SaCheckPermission("userRole:batch:delete")
    public CommonResponse deleteUserRoles(@RequestParam("ids") String ids) {
        return CommonResponse.success(userRoleService.deleteUserRoles(ids), "批量删除成功");
    }

    @ApiOperation(value = "查询")
    @ApiImplicitParam(name = "id", value = "用户角色关联id", paramType = "path", dataType = "Integer", required = true)
    @GetMapping("/{id}")
    @ControllerLog(action = "查询用户角色关联", message = "查询用户角色关联失败")
    // @SaCheckPermission("userRole:get")
    public CommonResponse getUserRole(@PathVariable("id") Integer id) {
        return CommonResponse.success(userRoleService.getUserRole(id), "查询成功");
    }

    @ApiOperation(value = "查询列表")
    @GetMapping("/list")
    @ControllerLog(action = "查询用户角色关联列表", message = "查询用户角色关联列表失败")
    // @SaCheckPermission("userRole:list")
    public CommonResponse listUserRoles(CommonRequest commonRequest, UserRole userRole) {
        return CommonResponse.success(userRoleService.listUserRoles(commonRequest, userRole), "查询成功");
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/pages")
    @ControllerLog(action = "分页查询用户角色关联", message = "分页查询用户角色关联失败")
    // @SaCheckPermission("userRole:page")
    public CommonResponse listPageUserRoles(CommonRequest commonRequest, UserRoleDTO userRoleQuery) {
        return CommonResponse.success(userRoleService.listPageUserRoles(commonRequest, userRoleQuery), "查询成功");
    }

    @ApiOperation(value = "分页查询角色可选用户")
    @GetMapping("/user/pages")
    @ControllerLog(action = "分页查询角色可选用户", message = "分页查询角色可选用户失败")
    // @SaCheckPermission("userRole:user:page")
    public CommonResponse listPageUsers(CommonRequest commonRequest, UserRoleDTO userRoleQuery) {
        return CommonResponse.success(userRoleService.listPageUsers(commonRequest, userRoleQuery), "查询成功");
    }

    @ApiOperation(value = "批量新增")
    @PostMapping("/batches")
    @ControllerLog(action = "批量新增用户角色关联", message = "批量新增用户角色关联失败")
    // @SaCheckPermission("userRole:batch:add")
    public CommonResponse insertBatchUserRoles(@Valid @RequestBody List<UserRole> userRoleList) {
        return CommonResponse.success(userRoleService.insertBatchUserRoles(userRoleList), "新增成功");
    }
}
